package workbench;
import box2D.collision.shapes.B2CircleShape;
import box2D.common.math.B2Vec2;
import box2D.dynamics.B2BodyDef;
import box2D.dynamics.B2DebugDraw;
import box2D.dynamics.B2FixtureDef;
import box2D.dynamics.B2World;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.geom.Point;
import openfl.Vector;
import workbench.actors.Ball;
import workbench.actors.ContactListener;
import workbench.actors.DummyBall;
import workbench.actors.HillLeft;
import workbench.actors.HillRight;
import workbench.actors.Mid;

import com.funkyfurrymonkey.world2d.StaticActor;
import box2D.dynamics.B2Body;
/**
 * ...
 * @author Marcos Martinez
 */
class Workbench extends Sprite
{
	
	private var m_cGravity:B2Vec2;
	private var m_cWorld:B2World;
	private var m_cParent:Sprite;
	
	private var PHYSIC_SCALE:Float = 1  / 3;

	
	
	var m_cBall:Ball;
	public function new(cParent:Sprite) 
	{
		super();
		m_cParent = cParent;
	}
	
	public function init()
	{
		//create world
		m_cGravity = new B2Vec2(0, 10);
		m_cWorld = new B2World(m_cGravity, true);
		
		setDebugDraw();
		m_cParent.stage.addEventListener(Event.ENTER_FRAME, movingWorld);		
		createGround();
		createGround2();
		createBall();
		
	}
	
	private function createGround() 
	{
		var cCastleHillLeft:HillLeft = new HillLeft(m_cWorld);
		cCastleHillLeft.start( new Bitmap( Assets.getBitmapData("img/grassHillLeft.png")), new Point(300, 120));
		this.addChild(cCastleHillLeft.m_cCanvas);
		
			var cCastleHillLeft2:HillLeft = new HillLeft(m_cWorld);
		cCastleHillLeft2.start( new Bitmap( Assets.getBitmapData("img/grassHillLeft.png")), new Point(230, 190));
		this.addChild(cCastleHillLeft2.m_cCanvas);
	}
		
			
	private function createGround2() 
	{
		var cDirtHillRight:HillRight = new HillRight(m_cWorld);
		cDirtHillRight.start( new Bitmap( Assets.getBitmapData("img/grassHillRight.png")), new Point(170, 420));
		this.addChild(cDirtHillRight.m_cCanvas);
		
		var cGrassMid :Mid = new Mid(m_cWorld);
		cGrassMid.start(new Bitmap( Assets.getBitmapData("img/grassMid.png")), new Point(100, 420));
		this.addChild(cGrassMid.m_cCanvas);
	}
	
	private function createBall()
	{
		
		 m_cBall = new Ball(m_cWorld, new Bitmap(Assets.getBitmapData("img/hud_p1.png")));
		 this.addChild(m_cBall.m_cCanvas);
		 
		 
	}
	
	private function setDebugDraw():Void
	{
		// set debug draw
		var dbgDraw:B2DebugDraw = new B2DebugDraw();
		
		dbgDraw.setSprite(this);
		dbgDraw.setDrawScale(PHYSIC_SCALE);
		dbgDraw.setFillAlpha(0.3);
		dbgDraw.setLineThickness(1.0);
		dbgDraw.setFlags(B2DebugDraw.e_shapeBit | B2DebugDraw.e_jointBit);
		m_cWorld.setDebugDraw(dbgDraw);
	}
	
	private function movingWorld(ev:Event):Void
	{
		m_cWorld.step(0.025 , 20, 10);
		m_cWorld.drawDebugData();
		
		m_cBall.updateNow();
		m_cWorld.clearForces();
	}
}